package com.seven.dlandemo.ui;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;

import com.seven.dlandemo.BR;
import com.seven.dlandemo.R;
import com.seven.dlandemo.databinding.ActivityWirelessDisplayBinding;

public class WirelessDisplayActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.myTAG + WirelessDisplayActivity.class.getSimpleName();
    public static final String EXTRA_VIDEO_URL = "video_url";

    private ActivityWirelessDisplayBinding mViewBinding;
    private WirelessDisplayViewModel mViewModel;
    private String mVideoUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mVideoUrl = getIntent().getStringExtra(EXTRA_VIDEO_URL);
        if (TextUtils.isEmpty(mVideoUrl)) {
            finish();
        }
        mViewBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_wireless_display, null, false);
        setContentView(mViewBinding.getRoot());
        mViewModel = new WirelessDisplayViewModel(this, mViewBinding);
        mViewModel.setVideoUrl(mVideoUrl);
        mViewBinding.setVariable(BR.WirelessDisplayViewModel, mViewModel);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mVideoUrl = intent.getStringExtra(EXTRA_VIDEO_URL);
        if (TextUtils.isEmpty(mVideoUrl)) {
            finish();
        }else {
            // todo
        }
    }

    @Override
    protected void onDestroy() {
        if (mViewModel != null) {
            mViewModel.onDestroy();
        }
        super.onDestroy();
    }

    public static void start(Context context, String videoUrl) {
        if (TextUtils.isEmpty(videoUrl)) {
            Log.e(TAG, "video url is empty");
            return;
        }
        Intent intent = new Intent(context, WirelessDisplayActivity.class);
        intent.putExtra(EXTRA_VIDEO_URL, videoUrl);
        context.startActivity(intent);
    }

}